<?php

/* 
 * 商家模型
 * @author wyb
 */
namespace Qwadmin\Model;
class YtoApiModel extends ApiModel {
   
   public $username='';
   public $password='';
   public $method='';
   function __construct()
   {   
        ///http://open.yto.net.cn:8007/OpenPlatform/home 
       $this->url="http://58.32.246.71:8000/CommonOrderModeBPlusServlet.action";
       $this->clientId='K21000119'; 
       $this->password='123456';   
      // $this->username='ok'; 
      // $this->password='python'; 
   }
   
   public function md516($str)
   {
	   return substr(md5($str),8,16);
   }   
	
   public function test()
   {
	   $this->__construct();
	   $logistics_interface='<RequestOrder> <clientID>K21000119</clientID> <logisticProviderID>YTO</logisticProviderID> <customerId> K21000119</customerId> <txLogisticID>WP15183117908113</txLogisticID> <tradeNo>1</tradeNo> <totalServiceFee>1</totalServiceFee> <codSplitFee>1</codSplitFee> <orderType>1</orderType> <serviceType>1</serviceType> <flag>1</flag> <sender> <name>发件人姓名</name> <postCode>526238</postCode> <phone>021-12345678</phone> <mobile>18112345678</mobile> <prov>上海</prov> <city>上海市,青浦区</city> <address>华徐公路3029弄28号</address> </sender> <receiver> <name>收件人姓名</name> <postCode>0</postCode> <phone>0</phone> <mobile>18089666766</mobile> <prov>上海</prov> <city>上海市,青浦区</city> <address>华徐公路3029弄28号</address> </receiver> <sendStartTime>2015-12-12 12:12:12</sendStartTime> <sendEndTime>2015-12-12 12:12:12</sendEndTime> <goodsValue>1</goodsValue> <items> <item> <itemName>商品</itemName> <number>2</number> <itemValue>0</itemValue> </item> </items> <insuranceValue>1</insuranceValue> <special>1</special> <remark>1</remark> </RequestOrder>';
	   $xml=simplexml_load_string($logistics_interface);
 
	   $ds['logistics_interface']=$logistics_interface;
	   $ds['data_digest']=urlencode(base64_encode(md5($logistics_interface.$this->password,true)));
	   $ds['type']='offline';
	   $ds['clientId']=$this->clientId;
	   $dstr=http_build_query($ds);
	   //echo urlencode("<order></order>")."<br>";
	   echo  base64_encode(md5("<order></order>123456",true))."<br/>";
	   echo  strtoupper($this->md516("<order></order>123456"))."<br/>";
	  
	   echo  base64_encode(strtoupper($this->md516("<order></order>123456")))."<br/>";
	  
	   $re=$this->post($this->url,http_build_query($ds));
	
	
	var_dump($re);
    die();
	   
   }
   public function supplier_add($id=1)
   {
	   $this->__construct();
	   $modol=M('suplier');
	   $data=$modol->where("erp_id is null or erp_id =''")->limit(1)->find();
	   $data['username']=$this->username;
	   $data['passwd']=$this->password;
	   $data['method']='erp.supplier.add';
	   $data['phone']=$data['tel'];
	  
        $spid=$data['id'];
	  ///过滤掉无需提交的数据
	  unset ($data['user']);
	  unset ($data['tel']);
	  unset ($data['id']);
	  unset ($data['erp_id']);
	 
	  
	  var_dump($data);
      
	  $result=$this->post_json($this->url,$data);
	  ///$result->error_response
	  echo "<br>";
	  $re=json_decode($result);
	  echo $re->erp_supplier_add_response->data;
	  ///回写供应商ID
      if(is_int($re->erp_supplier_add_response->data))
	  {
		  $da['id']=$spid;
		  $da['erp_id']=$re->erp_supplier_add_response->data;
		  $modol->where('id='.$spid)->data($da)->save();
		  echo "saved erpid <br>";
	  };
	  var_dump($result);
	  ////继续处理未处理完成的
	  if($data&&is_int($re->erp_supplier_add_response->data))
	   echo '<script type="text/javascript"> function refresh(){location.href=location.href;window.location.reload();};setTimeout(refresh(),3000)</script><br>';
      
	   
   }
   public function get_order($out_sn='',$buyer='')
   {
	   
	   
	   $this->__construct();
	   $ds['username']=$this->username;
	   $ds['passwd']=$this->password;
	   $ds['method']='erp.tradeOrder.get';
	   $ds['order']=$out_sn;
	   $ds['order']=$buyer;
	   $result=$this->post_json($this->url,$ds);
	   
	   var_dump($result);
	   
   }
   
   
      public function erp_hsTaxRate_get ($sku='')
   {
	   
	   
	   $this->__construct();
	   $ds['username']=$this->username;
	   $ds['passwd']=$this->password;
	   $ds['method']='erp.hsTaxRate.get';
	   $ds['sku']=$sku;
	   $result=$this->post_json($this->url,$ds);
	   
	   var_dump($result);
	   
   }
   public function goods_reg($id=1)
   {
	   
		              // 首先开启session
        //$_SESSION['needle']=1170;
		if(isset($_SESSION['needle']))$_SESSION['needle']=$_SESSION['needle']-1;
	    $this->__construct();
		$modol=M('goods');
		$ds['username']=$this->username;
	    $ds['passwd']=$this->password;
		/*  税务信息获取
		$ds['skuList']=array('P0000053');
		$ds['method']='erp.hsTaxRate.get';
		$result=$this->post_json($this->url,$ds);
		
		var_dump($result);die(); */
		$ds['method']='erp.goods.add';

		
		$data=$modol->where("sku is null or sku =''")->limit(1)->find();
		if(!$data){echo "no date need to proccessed";die();} 
        $goods_id=$data['id'];
		$ds['code']=$data['earn13'];
		$ds['name']=$data['name'];
		$ds['cat_code']='002';
		$supply_id=$this->get_supplyid_byearn13($data['earn13']);
		echo "供应商：".$supply_id."<br>";
		$ds['id_card']=$supply_id; ///供应商编码erp_supply_id
		$ds['brand_code']='SC2016';
		echo "提交数据：";
		var_dump($ds);
		$result=$this->post_json($this->url,$ds);
		echo "<br>";
		
		$re=json_decode($result);
		var_dump($re);
		echo "<br>";
	    echo $re->erp_supplier_add_response->data;
	  ///回写供应商ID
      if($re->erp_goods_add_response->data)
	  {
		  $da['id']=$goods_id;
		  $da['erp_id']=$re->erp_goods_add_response->data->goods;
		  $da['sku']=$re->erp_goods_add_response->data->sku_list[0]->code;
		  $da['supply_id']=$supply_id;
		  $da['lastmodify']=time();
		  
		  var_dump($da);
		  $modol->where('id='.$goods_id)->data($da)->save();
		  echo "<br>saved goods_info <br>";
	  };
	  
	  if($data&&$re->erp_goods_add_response->data)
	   echo '剩余'.$_SESSION['needle'].'等待进入下一个商品的ERP注册。'.'<script type="text/javascript"> function refresh(){location.href=location.href;window.location.reload();};setTimeout(refresh(),300)</script><br>';
	  if(!$re)
       	 echo '接口错误5秒后重新刷新。'.'<script type="text/javascript"> function refresh(){location.href=location.href;window.location.reload();};setTimeout(refresh(),5000)</script><br>';	  
	   
   }
   
   
   
   public function get_supplyid_byearn13($en13)
   {
	  $model=M('spzl');
      $data=$model->where(' code ='.$en13)->find();
      //$supplier=M('suplier');
	  //$sup=$supplier->where("code = 'sc".$data['supply_id']."' ")->find();
      return 'sc'.$data['supply_id'];
   }
   
   public function ordersyn($goods=0)
   {
	

    
  	
	$goods=array (
  'order' => 
  array (
    'ref' => '2016110410647612',
    'channel_code' => 'b2b',
    'trade_pattern' => 'b1',
    'date_order' => '2016-11-04 10:23:24',
    'buyer' => '555@qq.com',
    'seller' => 'dys@newbeemall.com',
    'receive_info' => '邓娟|18223501120|中国|重庆|重庆市|江北区|重庆市江北区洋河路东和城5栋11-13|400000',
    'total_import_taxes' => '7.310',
    'amount_untaxed' => '55.400',
    'amount_total' => '68.710',
    'delivery_fee' => '6.000',
    'buyer_reg_no' => '555@qq.com',
    'buyer_name' => '邓娟',
    'buyer_id_type' => '1',
    'buyer_id_card' => '510214197411202020',
    'order_line' => 
    array (
      0 => 
      array (
        'sku' => 'P0043661',
        'name' => '(保税)日本 菊正宗 日本酒化粧水高保湿款 500ml',
        'price' => 36.7,
        'num' => 1,
        'tax_fee' => '4.840',
        'sub_total' => '41.540',
        'sub_total_untaxed' => 36.7,
      ),
      1 => 
      array (
        'sku' => 'P0043480',
        'name' => '(保税)日本 LOSHI 北海道马油面霜 220g',
        'price' => 18.7,
        'num' => 1,
        'tax_fee' => '2.466',
        'sub_total' => '21.166',
        'sub_total_untaxed' => 18.7,
      ),
    ),
    'payment_line' => 
    array (
      0 => 
      array (
        'payment_account' => 'alipay',
        'payamount' => 68.71,
        'payment_no' => '2016110410647612',
      ),
    ),
    'ebpCode' => '50056602FC',
  ),
);
     $goods['method']='erp.trade.add';
      $goods['username']=$this->username;
	    $goods['passwd']=$this->password;

	 $result=$this->post_json($this->url,$goods);
     echo "<br>result:";
     var_dump($result);
	 echo "<br>";
   }
      
}

